%--------------------------------------------------------------------------
% Script to generate impulse responses
%--------------------------------------------------------------------------

%--------------------------------------------------------------------------
% Housekeeping
%--------------------------------------------------------------------------

clear; 
clc;
close all

set(0,'defaultTextInterpreter','latex');

% set specs 
xmin  = 0;
xmax  = 2.6;
xn    = 100;
xgrid = linspace(xmin, xmax, xn);

% set specs
nDataSel  = '1';
figDir    = [pwd, '/', 'Figures' ,'/Para', nDataSel ,'/'];
[~, ~, ~] = mkdir(figDir);

%%
%--------------------------------------------------------------------------
% Load population IRFs
%--------------------------------------------------------------------------

popDir    = [pwd, '/Data/Para', nDataSel ,'/'];
pop_pctl_IRF = csvread( [popDir, 'IRF_aK_percs_employed.csv'], 0, 0);


%--------------------------------------------------------------------------
% Load IRFs from functional VAR.
%--------------------------------------------------------------------------

% set specs 
nfVARSpec = '3';
nVARSpec  = '3';
nMCMCSpec = '1';
nMod      = 'fVAR';

n_drawsread = 200; % set how many posterior draws to consider

sNamefVAR = ['fVAR', nfVARSpec, '_VAR', nVARSpec, '_MCMC', nMCMCSpec];

% load IRFs
% sh_id = 1: TFP shock
irfDir = [pwd, '/', 'results' ,'/Para', nDataSel ,'/', sNamefVAR, '/'];
pctl_IRF = csvread( [irfDir, sNamefVAR, '_IRF_Pctl_AggSh1_pmean.csv'], 1, 0); 
[H, pctl_all] = size(pctl_IRF);
H = H-1;

pctl_IRF_uncertainty = zeros(H+1, pctl_all, n_drawsread);

for pp = 1:n_drawsread
    pctl_IRF_uncertainty(:,:,pp)=csvread( [irfDir, sNamefVAR, '_IRF_Pctl_AggSh1_', num2str(pp), '.csv'], 1, 0);
end

%--------------------------------------------------------------------------
% Load IRFs from AltVAR.
%--------------------------------------------------------------------------

% set specs 
sNameAltVAR = 'AltVAR3P_MCMC1_Pctl';
n_drawsread = 200; % set how many posterior draws to consider


% load IRFs
% sh_id = 1: TFP shock
irfDir = [pwd, '/', 'results' ,'/Para', nDataSel ,'/', sNameAltVAR, '/'];
AltVAR_IRF = csvread( [irfDir, sNameAltVAR, '_IRF_YY_AggSh1_pmean.csv'], 1, 0); 
[H, n_all] = size(AltVAR_IRF);
n_agg   = 2;
n_cross = n_all - 2;
H = H-1;

AltVAR_IRF_uncertainty = zeros(H+1, n_all, n_drawsread);

for pp = 1:n_drawsread
    AltVAR_IRF_uncertainty(:,:,pp)=csvread( [irfDir, sNameAltVAR, '_IRF_YY_AggSh1_', num2str(pp), '.csv'], 1, 0);
end


%%
for ii = 1:pctl_all
    
    pctl_IRF_fVAR_ii  = squeeze(pctl_IRF_uncertainty(:,ii,:));
    pctl_IRF_fVAR_ii  = pctl_IRF_fVAR_ii - pctl_IRF_fVAR_ii(1,:);
    pctl_IRF_fVAR_q10 = quantile(pctl_IRF_fVAR_ii,0.1,2);
    pctl_IRF_fVAR_q50 = quantile(pctl_IRF_fVAR_ii,0.5,2);
    pctl_IRF_fVAR_q90 = quantile(pctl_IRF_fVAR_ii,0.9,2);

    pctl_IRF_AltVAR_ii  = squeeze(AltVAR_IRF_uncertainty(:,n_agg+ii,:));
    pctl_IRF_AltVAR_ii  = pctl_IRF_AltVAR_ii - pctl_IRF_AltVAR_ii(1,:);
    pctl_IRF_AltVAR_q10 = quantile(pctl_IRF_AltVAR_ii,0.1,2);
    pctl_IRF_AltVAR_q50 = quantile(pctl_IRF_AltVAR_ii,0.5,2);
    pctl_IRF_AltVAR_q90 = quantile(pctl_IRF_AltVAR_ii,0.9,2);
    
    hh = 59;

    figure(ii);clf;
    set(figure(ii),'PaperType','usletter','PaperOrientation','Landscape','PaperPosition',[0.1 0.1 11 8.5]);
    plot(-1:hh,pctl_IRF_fVAR_q10(1:hh+2,1),'Color','b','LineStyle',':','LineWidth',2)  
    hold on
    plot(-1:hh,pctl_IRF_fVAR_q50(1:hh+2,1),'Color','b','LineStyle','-','LineWidth',4) 
    hold on
    plot(-1:hh,pctl_IRF_fVAR_q90(1:hh+2,1),'Color','b','LineStyle',':','LineWidth',2) 
    hold on
    plot(-1:hh,(pop_pctl_IRF(ii,1:hh+2)-pop_pctl_IRF(ii,1))','Color','k','LineStyle',':','LineWidth',4) 
    hold on
    plot(-1:hh,pctl_IRF_AltVAR_q10(1:hh+2,1),'Color','r','LineStyle',':','LineWidth',2)  
    hold on
    plot(-1:hh,pctl_IRF_AltVAR_q50(1:hh+2,1),'Color','r','LineStyle','-','LineWidth',4) 
    hold on
    plot(-1:hh,pctl_IRF_AltVAR_q90(1:hh+2,1),'Color','r','LineStyle',':','LineWidth',2) 
    hold on
    plot(-1:hh,(pop_pctl_IRF(ii,1:hh+2)-pop_pctl_IRF(ii,1))','Color','k','LineStyle','-','LineWidth',4) 
    hold on
    plot(-1:hh, zeros(size(-1:hh)), 'Color', 'k', 'LineStyle', '-', 'LineWidth',2)
    set(gca,'FontSize',50)
    xlim([-1 hh])

    sFigName = [sNamefVAR, '_', sNameAltVAR, '_IRF_AggSh1_pctl', num2str(ii),'.pdf'];
    saveas(figure(ii), [figDir sFigName] );
%     close all

end



